package com.esyoga.yoyoga.service.impl;
import com.esyoga.yoyoga.mapper.TStudentMapper;
import com.esyoga.yoyoga.pojo.TStudent;
import com.esyoga.yoyoga.pojo.TStudentExample;
import com.esyoga.yoyoga.service.StudentVerifyService;
import lombok.Data;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 用户验证
 * @author YangTao
 */
@Data
@Service
public class StudentVerifyServiceServiceImpl implements StudentVerifyService {
    @Resource
    private TStudentMapper tStudentMapper;
    /**
     * 学员注册
     *
     * @return
     */
    @Override
    public int studentRegister(String phoneNumber,String password) {
        TStudent tStudent = new TStudent();
        tStudent.setsAccount(phoneNumber);
        tStudent.setsPassword(password);
        tStudent.setsImage("/image");
        int i = tStudentMapper.insertSelective(tStudent);
        return i;
    }

    /**
     * 学员登录
     *
     * @return
     */
    @Override
    public String studentLogin() {
        return null;
    }

    /**
     * 用户绑定手机号
     *
     * @return
     */
    @Override
    public String bindingPhone() {
        return null;
    }

    /**
     * 学员重置密码
     *
     * @return
     */
    @Override
    public int resetPassword(String restPhone,String restPassword) {
        TStudentExample tStudentExample = new TStudentExample();
        TStudentExample.Criteria criteria = tStudentExample.createCriteria();
        criteria.andSAccountEqualTo(restPhone);
        TStudent tStudent = new TStudent();
        tStudent.setsAccount(restPhone);
        tStudent.setsPassword(restPassword);
        int i = tStudentMapper.updateByExampleSelective(tStudent, tStudentExample);
        return i;
    }

    /**
     * 通过学员手机号查密码
     *
     * @param phone
     * @return
     */
    @Override
    public String findPasswordByPhone(String phone) {

        String password = null;
        TStudentExample tStudentExample = new TStudentExample();
        //通过Criteria构造查询条件
        TStudentExample.Criteria criteria = tStudentExample.createCriteria();
        criteria.andSAccountEqualTo(phone);
        List<TStudent> list = tStudentMapper.selectByExample(tStudentExample);

        if(list.size()!=0){
            TStudent tStudent = list.get(0);
            password = tStudent.getsPassword();
        }
        return password;
    }

    /**
     * 通过手机号查学员信息
     *
     * @param phone
     * @return
     */
    @Override
    public TStudent findTstudentByPhone(String phone) {
        TStudent tStudent = null;
        TStudentExample tStudentExample = new TStudentExample();
        //通过Criteria构造查询条件
        TStudentExample.Criteria criteria = tStudentExample.createCriteria();
        criteria.andSAccountEqualTo(phone);
        List<TStudent> list = tStudentMapper.selectByExample(tStudentExample);
        if(list.size()!=0){
           tStudent = list.get(0);
        }
        return tStudent;
    }

    /**
     * 完善用户信息
     *
     * @param tStudent
     * @return
     */
    @Override
    public int studentAddInfo(TStudent tStudent) {
        TStudentExample tStudentExample = new TStudentExample();
        TStudentExample.Criteria criteria = tStudentExample.createCriteria();
        criteria.andSAccountEqualTo(tStudent.getsAccount());
        int i = tStudentMapper.updateByExampleSelective(tStudent, tStudentExample);
        return i;
    }

}
